package com.moxiao.yfjh.module.activity.controller.admin.packageProduct;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;

import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;

import com.moxiao.yfjh.module.activity.controller.admin.packageProduct.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageProduct.PackageProductDO;
import com.moxiao.yfjh.module.activity.convert.packageProduct.PackageProductConvert;
import com.moxiao.yfjh.module.activity.service.packageProduct.PackageProductService;

@Tag(name = "管理后台 - 相亲套餐商品")
@RestController
@RequestMapping("/activity/package-product")
@Validated
public class PackageProductController {

    @Resource
    private PackageProductService packageProductService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲套餐商品")
    @PreAuthorize("@ss.hasPermission('activity:package-product:create')")
    public CommonResult<Boolean> createPackageProduct(@Valid @RequestBody List<PackageProductCreateReqVO> createReqVO) {
        packageProductService.createPackageProduct(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲套餐商品")
    @PreAuthorize("@ss.hasPermission('activity:package-product:update')")
    public CommonResult<Boolean> updatePackageProduct(@Valid @RequestBody List<PackageProductUpdateReqVO> updateReqVO) {
        packageProductService.updatePackageProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲套餐商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:package-product:delete')")
    public CommonResult<Boolean> deletePackageProduct(@RequestParam("id") Long id) {
        packageProductService.deletePackageProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲套餐商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:package-product:query')")
    public CommonResult<PackageProductRespVO> getPackageProduct(@RequestParam("id") Long id) {
        PackageProductDO packageProduct = packageProductService.getPackageProduct(id);
        return success(PackageProductConvert.INSTANCE.convert(packageProduct));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲套餐商品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:package-product:query')")
    public CommonResult<List<PackageProductRespVO>> getPackageProductList(@RequestParam("ids") Collection<Long> ids) {
        List<PackageProductDO> list = packageProductService.getPackageProductList(ids);
        return success(PackageProductConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲套餐商品分页")
    @PreAuthorize("@ss.hasPermission('activity:package-product:query')")
    public CommonResult<PageResult<PackageProductRespVO>> getPackageProductPage(@Valid PackageProductPageReqVO pageVO) {
        PageResult<PackageProductDO> pageResult = packageProductService.getPackageProductPage(pageVO);
        return success(PackageProductConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲套餐商品 Excel")
    @PreAuthorize("@ss.hasPermission('activity:package-product:export')")
    @OperateLog(type = EXPORT)
    public void exportPackageProductExcel(@Valid PackageProductExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PackageProductDO> list = packageProductService.getPackageProductList(exportReqVO);
        // 导出 Excel
        List<PackageProductExcelVO> datas = PackageProductConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲套餐商品.xls", "数据", PackageProductExcelVO.class, datas);
    }

}
